import { Request, Response, NextFunction } from 'express';
import { generateRSS2, generateAtom, generateJSON } from '@src/util/rss-genertor';

/**
 * 生成 RSS 2.0 格式的订阅
 */
export const getRSS = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const rss = await generateRSS2();
    res.set('Content-Type', 'application/rss+xml; charset=utf-8');
    res.send(rss);
  } catch (error) {
    next(error);
  }
};

/**
 * 生成 Atom 1.0 格式的订阅
 */
export const getAtom = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const atom = await generateAtom();
    res.set('Content-Type', 'application/atom+xml; charset=utf-8');
    res.send(atom);
  } catch (error) {
    next(error);
  }
};

/**
 * 生成 JSON Feed 格式的订阅
 */
export const getJSON = async (req: Request, res: Response, next: NextFunction) => {
  try {
    const json = await generateJSON();
    res.set('Content-Type', 'application/json; charset=utf-8');
    res.send(json);
  } catch (error) {
    next(error);
  }
};
